package cn.jiyun.service.impl;

import cn.jiyun.entity.XmAuditbusiness;
import cn.jiyun.entity.XmSettled;
import cn.jiyun.mapper.XmAuditbusinessMapper;
import cn.jiyun.mapper.XmSettledMapper;
import cn.jiyun.service.IXmAuditbusinessService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-25
 */
@Service
public class XmAuditbusinessServiceImpl extends ServiceImpl<XmAuditbusinessMapper, XmAuditbusiness> implements IXmAuditbusinessService {


    @Autowired
    private XmSettledMapper xmSettledMapper;
    @Override
    public Page<XmAuditbusiness> findAll(Integer current, Integer pageSize, XmAuditbusiness xmAuditbusiness) {
        Page<XmAuditbusiness> xmSettledPage = new Page<>(current, pageSize);
        QueryWrapper<XmAuditbusiness> xmAuditbusinessQueryWrapper = new QueryWrapper<>();
        if(xmAuditbusiness.getSerialNumber()!=null){
            xmAuditbusinessQueryWrapper.like("serial_number",xmAuditbusiness.getSerialNumber());
        }
        if(xmAuditbusiness.getBid()!=null){
            xmAuditbusinessQueryWrapper.like("did",xmAuditbusiness.getBid());
        }
        if(xmAuditbusiness.getBname()!=null && !xmAuditbusiness.getBname().equals("")){
            xmAuditbusinessQueryWrapper.like("bname",xmAuditbusiness.getBname());
        }
        if(xmAuditbusiness.getType()!=null){
            xmAuditbusinessQueryWrapper.like("type",xmAuditbusiness.getType());
        }
        if (xmAuditbusiness.getZtime()!= null) {
            LocalDateTime now = LocalDateTime.now();
            int mo = 90;
            if (xmAuditbusiness.getZtime() == 1) {
                xmAuditbusinessQueryWrapper.apply("ABS(DATEDIFF({0}, registration)) <= {1}", now, mo);
            }

            if(xmAuditbusiness.getZtime()==2){
                xmAuditbusinessQueryWrapper.apply("YEAR(registration)=YEAR(CURRENT_DATE)");
            }

            if(xmAuditbusiness.getZtime()==3){
                xmAuditbusinessQueryWrapper.apply("DATE_FORMAT(registration,'%Y')=2022");
            }

            if(xmAuditbusiness.getZtime()==4){
                xmAuditbusinessQueryWrapper.apply("DATE_FORMAT(registration,'%Y')=2021");
            }
            if(xmAuditbusiness.getZtime()==5){
                xmAuditbusinessQueryWrapper.apply("DATE_FORMAT(registration,'%Y')=2020");
            }
            if(xmAuditbusiness.getZtime()==6){
                xmAuditbusinessQueryWrapper.apply("DATE_FORMAT(registration,'%Y')<2020");
            }
        }
        if (xmAuditbusiness.getStatus() != null) {
            xmAuditbusinessQueryWrapper.eq("status", xmAuditbusiness.getStatus());
        }
        if (xmAuditbusiness.getRefuse() != null) {
            xmAuditbusinessQueryWrapper.eq("refuse", xmAuditbusiness.getRefuse());
        }
        return baseMapper.selectPage(xmSettledPage, xmAuditbusinessQueryWrapper);
    }


    @Override
    public void updateAudit(XmAuditbusiness xmAuditbusiness) {
        if(xmAuditbusiness.getStatus().equals("4")){
            baseMapper.updateById(xmAuditbusiness);
        }else if (xmAuditbusiness.getStatus().equals("3")){
            baseMapper.updateById(xmAuditbusiness);
            XmSettled xmSettled = xmSettledMapper.selectById(xmAuditbusiness.getBid());
            if(xmAuditbusiness.getType()==1){
                xmSettled.setImg(xmAuditbusiness.getNewInformation());
            }
            else if (xmAuditbusiness.getType()==2){
                xmSettled.setMain(xmAuditbusiness.getNewInformation());
            }
            else if(xmAuditbusiness.getType()==3){
                xmSettled.setFacadeImg(xmAuditbusiness.getNewInformation());
            }else if(xmAuditbusiness.getType()==4){
                xmSettled.setEnvironmentImg(xmAuditbusiness.getNewInformation());
            }else if(xmAuditbusiness.getType()==5){
                xmSettled.setName(xmAuditbusiness.getNewInformation());
            }
            xmSettledMapper.updateById(xmSettled);
        }
    }
}
